package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.pojo.CommoditySpecification;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.fuhe.chen.vendingmachine.service.ICommoditySpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格管理
 * 新增商品规格/修改商品规格/删除商品规格
 */

@Controller
@Api(tags = "商品规格信息管理")
@RequestMapping("/admin/commoditySpecification")
public class CommoditySpecificationController {

    @Autowired
    ICommoditySpecificationService commoditySpecificationService;

    @Autowired
    ICommodityService commodityService;

    @ApiOperation(value = "根据商品id查询商品规格")
    @GetMapping("/queryCommSpecification/{commodityId}")
    @ResponseBody
    public ApiResponseForList<CommoditySpecification> queryCommSpecification(@PathVariable("commodityId") Integer commodityId) {

        List<CommoditySpecification> commoditySpecifications = commoditySpecificationService.queryCommoditySpecification(commodityId);

        ApiResponseForList<CommoditySpecification> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("查询成功");
        response.setData(commoditySpecifications);

        return response;
    }

    @ApiOperation(value = "新增商品规格")
    @PostMapping("/addCommSpecification")
    @ResponseBody
    @AddLog(operation = "新增商品规格")
    public ApiResponseForList<String> addCommSpecification(@RequestParam("commodityId") Integer commodityId,
                                                           @RequestParam("price") Float price,
                                                           @RequestParam("specification") String specification) {
        ApiResponseForList<String> response = new ApiResponseForList<>();

        Boolean isCommodityOnSale = Utils.isCommodityOnSale(commodityId,commodityService);
        if (isCommodityOnSale) {
            response.setSuccess(false);
            response.setCode(11111);
            response.setMessage("上架商品无法进行修改");
            return response;
        }

        CommoditySpecification commoditySpecification = new CommoditySpecification();
        commoditySpecification.setCommodity_id(commodityId);
        commoditySpecification.setPrice(price);
        commoditySpecification.setSpecifications(specification);

        commoditySpecificationService.addCommoditySpecification(commoditySpecification);

        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("添加成功");

        return response;
    }

    @ApiOperation(value = "修改商品规格")
    @PostMapping("/updateCommSpecification")
    @ResponseBody
    @AddLog(operation = "修改商品规格")
    public ApiResponseForList<String> updateCommSpecification(@RequestParam("id") Integer id,
                                                              @RequestParam("commodityId") Integer commodityId,
                                                              @RequestParam("price") Float price,
                                                              @RequestParam("specification") String specification) {
        ApiResponseForList<String> response = new ApiResponseForList<>();
        Boolean isCommodityOnSale = Utils.isCommodityOnSale(commodityId,commodityService);
        if (isCommodityOnSale) {
            response.setSuccess(false);
            response.setCode(11111);
            response.setMessage("上架商品无法进行修改");
            return response;
        }

        CommoditySpecification commoditySpecification = new CommoditySpecification();
        commoditySpecification.setId(id);
        commoditySpecification.setPrice(price);
        commoditySpecification.setSpecifications(specification);

        commoditySpecificationService.updateCommoditySpecification(commoditySpecification);

        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("修改成功");

        return response;
    }

    @ApiOperation(value = "删除商品规格")
    @PostMapping("/deleteCommSpecification")
    @ResponseBody
    @AddLog(operation = "删除商品规格")
    public ApiResponseForList<String> deleteCommSpecification(@RequestParam("id") Integer id,
                                                              @RequestParam("commodityId") Integer commodityId) {
        ApiResponseForList<String> response = new ApiResponseForList<>();
        Boolean isCommodityOnSale = Utils.isCommodityOnSale(commodityId,commodityService);
        if (isCommodityOnSale) {
            response.setSuccess(false);
            response.setCode(11111);
            response.setMessage("上架商品无法进行修改");
            return response;
        }

        commoditySpecificationService.deleteCommoditySpecification(id);

        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("删除成功");

        return response;
    }


}
